home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 013 / td3.arc / TD3.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1986-04-25  |  31.5 KB  |  1,347 lines

  1. {$R-,C-,U-}
  2. program td;
  3.    { version 3.12  Copright (c) 1986 by Mark Johnson 04/24/86           }
  4.    { Mark E. Johnson                 2272-F Benson Avenue               }
  5.    {                                 St. Paul Minnesota  55116          }
  6.    { evening phone                   612-698-3686                       }
  7.  
  8. { 3/15/86 - Text foreground colors added for IBM PC   }
  9. { 3/15/86 - Table is now in record format             }
  10. { 3/17/86 - Added windows, sound (errors cause beep)  }
  11. { 4/20/86 - Pull-Down windows added                   }
  12.  
  13. const
  14.  
  15.   MaxItems=20; {Max Items on a Menu Bar}
  16.   MaxMenus=10; {Max Menus}
  17.   Width=11;    {Width of Pull Down Fields}
  18.  
  19.   TBL_LEN = 80;  { maximum number of elements in report }
  20.  
  21. type
  22.   names        = string[80];
  23.   ltype        = string[85];
  24.   stype        = string[10];
  25.  
  26.   fieldrec     = record         { format of each report field }
  27.         rtype     : char;       { N)umeric, A)lpha, or L)iteral }
  28.         rio       : char;       { IO type, I)nput or O)utput  }
  29.         rname     : ltype;      { Field name                  }
  30.         rx        : integer;    { Line number                 }
  31.         ry        : integer;    { Column number               }
  32.         rlen      : integer;    { Length of field             }
  33.         rscale    : integer;    { Number of decimal places    }
  34.         rfgnd     : integer;    { forground color             }
  35.         rorder    : integer;    { Sequence number (for sort)  }
  36.   end;
  37.  
  38.    VideoMode =(Norm,Rev,Hi,Und,RevHi,Blink,BlinkHi,RevBlink,RevBlinkHi);
  39.    MaxString = String[255];
  40.    stringW = string[Width];
  41.  
  42.    ProtoMenu = record
  43.        NumEntry :array[0..MaxItems] of integer;
  44.        Menu:array[0..MaxItems] of array[0..MaxItems] of stringW;
  45.        MenuName:stringW;
  46.        NoItems:integer;
  47.    end;
  48.  
  49.    MenuPtr = ^ProtoMenu;
  50.    MenuAry =  array[1..MaxMenus] of MenuPtr;
  51.  
  52. var
  53.     junk      : char;
  54.     special   : boolean; { last character was special}
  55.  
  56.     fields    : array[1..TBL_LEN] of fieldrec;
  57.  
  58.     ndx       : integer;       { Position in Fields Table }
  59.     line      : ltype;         { input line for INFILE    }
  60.     lineno    : integer;       { The line we're on        }
  61.     colno     : integer;       { The column we're at      }
  62.     token     : ltype;         { used by parser           }
  63.     tail      : string[32];    { used by code generator   }
  64.     i,j,l     : integer;       { Misc. loop controls      }
  65.     incr      : integer;       { Field number increment   }
  66.     outtype   : char;          { Field output type        }
  67.     ans       : char;          { Query variable           }
  68.     infile    : text;          { input file               }
  69.     outfile   : text;          { output file              }
  70.     libfile   : text;          { library                  }
  71.     procname  : string[32];    { name of procedure        }
  72.     varfl     : boolean;       { include variable declarations? }
  73.     librfl    : boolean;       { include library file?    }
  74.     subrfl    : boolean;       { Write file as subroutine procedure? }
  75.     ctemp     : stype;
  76.     efile     : boolean;       { End of file flag         }
  77.     level     : integer;
  78.     inname    : string[15];    { SCR file name            }
  79.     outname   : string[15];    { PAS file name            }
  80.     libname   : string[15];    { LIB file name            }
  81.     lastcolor : integer;       { Current text color       }
  82.  
  83.     NumMenus  :integer;
  84.     Menus     :MenuAry;
  85.     exit      :boolean;
  86.     VideoSeg  :integer;{points to $B000 or $B800  for color or mono}
  87.     botbox    :maxstring;
  88.  
  89.     active,index,item,entry : integer;
  90.  
  91. label
  92.     generate, retry, endinp;
  93.  
  94.  
  95. function ColorMonitor:boolean;
  96. {returns TRUE if a Color monitor is installed}
  97. type regpack = record
  98.        ax,bx,cx,dx,bp,si,di,ds,es,flags:integer;end;
  99. var regs:regpack;
  100.    al:integer;
  101. begin
  102.   regs.ax:=15 shl 8;
  103.   intr($10,regs);
  104.   al:=Lo(regs.ax);
  105.   if al=$7 then ColorMonitor:=false else ColorMonitor:=true;
  106. end;
  107.  
  108.  
  109. Procedure SetVideoSeg;
  110. begin
  111.   if colormonitor then VideoSeg:=$B800 else VideoSeg:=$B000
  112. end;
  113.  
  114.  
  115. Procedure SetCursor(HiScan,LowScan:byte);
  116. type regpack = record
  117.        ax,bx,cx,dx,bp,si,di,ds,es,flags:integer;end;
  118. var regs:regpack;
  119. begin
  120.   regs.ax:=1 shl 8;
  121.   regs.cx:=HiScan shl 8 + LowScan;
  122.   intr($10,regs);
  123. end;
  124.  
  125.  
  126. Procedure CursorNormal;
  127. begin
  128.   if ColorMonitor then SetCursor(6,7) else  SetCursor(10,11);
  129. end;
  130.  
  131.  
  132. Procedure CursorBlock;
  133. begin
  134.   if ColorMonitor then SetCursor(1,7) else  SetCursor(1,14);
  135. end;
  136.  
  137.  
  138. Procedure CursorOff;
  139. begin
  140.   SetCursor(31,0);
  141. end;
  142.  
  143. procedure GetKb(var chcode,extcode:integer);
  144.  
  145. (*Obtains the character and extended codes of a struck key. The codes are
  146.  removed from the buffer. This procedure will wait for a keystrike if the
  147.  buffer is empty.*)
  148.  
  149. type
  150.   RegPack = record
  151.       ax,bx,cx,dx,di,si,ds,es,flags : integer;
  152.   end;
  153. var
  154.   regs:RegPack;
  155.  
  156. begin
  157.   regs.ax := $0000;
  158.   intr($16,regs);
  159.   extcode := regs.ax shr 8;     (*extended  code is AH*)
  160.   chcode := regs.ax and $00FF;  (*character code is AL*)
  161. end;
  162.  
  163.  
  164. function inchar(var ch:char;var ex:integer):boolean;{true if ASCII char}
  165. {Returns char and extended code from keyboard}
  166. var chcode,excode:integer;
  167.  
  168. begin
  169. getkb(chcode,ex);
  170. if chcode=0 then
  171.     begin
  172.     inchar:=false;
  173.     ch:=chr(ex);
  174.     end
  175. else
  176.     begin
  177.     ch:=chr(chcode);
  178.     inchar:=true;
  179.     if ex<>0 then
  180.       if chcode in [8,13,9,27] then
  181.       begin
  182.         ex:=chcode;
  183.         inchar:=false;
  184.       end;
  185.     end;
  186. end;{inchar}
  187.  
  188. procedure WriteAt(x,y:integer;WriteMode:VideoMode;TheString:maxstring);
  189. {Memory Mapped write}
  190. Var
  191.   i,j,k:integer;
  192.   Attribute:Byte;
  193.  
  194. Begin{1}
  195.   case WriteMode of {change these for color terminals}
  196.    Norm:       Attribute := $07;
  197.    Rev:        Attribute := $70;
  198.    Hi:         Attribute := $0F;
  199.    Und:        Attribute := $01;
  200.    RevHi:      Attribute := $78;
  201.    Blink:      Attribute := $87;
  202.    BlinkHi:    Attribute := $8F;
  203.    RevBlink:   Attribute := $F0;
  204.    RevBlinkHi: Attribute := $F8;
  205.    ELSE        Attribute := $07;{Normal}
  206.    end;
  207.  
  208.    j := 2*((y-1)*80+(x-1));{offset in video buffer}
  209.    i:=1;
  210.    k:=length(thestring);
  211.    While i<=k do
  212.        begin
  213.        Mem[VideoSeg : j] := Byte(TheString[i]);
  214.        Mem[VideoSeg : (j+1)] := Attribute;
  215.        i:=i+1;
  216.        j:=j+2;
  217.        end;
  218. end;              {1 of WriteAt}
  219.  
  220.  
  221. Procedure LoadMenus(var MenuList:MenuAry);
  222. {loads the menu data file}
  223. var i,j,k:integer;
  224.     f:text;
  225.     s:maxstring;
  226.  
  227. Procedure GetAMenu(var M:MenuPtr);
  228. label 99;
  229. var i,j,k:integer;
  230. begin
  231. i:=-1;
  232. j:=0;
  233. { s has been primed }
  234. M^.MenuName:=s;
  235. readln(f,s);
  236. s:=s+'            ';
  237. while (s[1]<>'*') and (not eof(f)) do
  238.    begin
  239.  
  240.    if s[1]<>' ' then
  241.      begin
  242.      if i>=0 then M^.NumEntry[i]:=j;
  243.      i:=i+1;
  244.      M^.Menu[i,0]:=s;
  245.      j:=0;
  246.      end
  247.  
  248.    else
  249.      if s[1]<>'*' then
  250.        begin
  251.        j:=j+1;
  252.        delete(s,1,1);
  253.        M^.Menu[i,j]:=s;
  254.        end
  255.      else goto 99;
  256.  
  257.   readln(f,s);
  258.   s:=s+'            ';
  259.  
  260.   end;
  261.  
  262. 99:
  263. M^.NumEntry[i]:=j;
  264. M^.NoItems:=i;
  265.  
  266. end;{GetAMenu}
  267.  
  268. begin{Load}
  269.  
  270. assign(f,'td.dat'); {alter name for application}
  271. reset(f);
  272.  
  273. i:=0;
  274. readln(f,s);
  275.  
  276. while not eof(f) do
  277.    begin
  278.    i:=i+1;
  279.    New(Menus[i]);
  280.    GetAMenu(Menus[i]);
  281.    end;
  282. NumMenus:=i;
  283.  
  284. close(f);
  285.  
  286. {some other initialization here}
  287.  
  288. botbox:='╚';
  289. for i:=1 to Width do botbox:=botbox+'═';
  290. botbox:=botbox+'╝';
  291.  
  292. end;{LoadMenu}
  293.  
  294.  
  295. procedure DoMenu(var itemsel,entrysel:integer;M:MenuPtr);
  296.  
  297.   {this runs a menu, reads keys etc,}
  298.   {itemsel and entrysel are returned}
  299.  
  300. type
  301.    setofkeys=set of 0..132;
  302.  
  303. var
  304.    chc,ex:integer;
  305.    ch:char;
  306.    validkeys:setofkeys;
  307.    asc,selection:boolean;
  308.    item,entry:integer;
  309.    s1,s2:maxstring;
  310.  
  311.  
  312. Procedure PaintMenuBar;
  313. var
  314.   i,sx:integer;
  315.  
  316. begin
  317. writeat(1,1,rev,
  318. '                                                                                ');
  319. for i:=0 to M^.NoItems do
  320.    begin
  321.    sx:=2+i*Width;
  322.    writeat(sx,1,rev,M^.Menu[i,0]);
  323.    end;
  324. end;{PaintMenuBar}
  325.  
  326.  
  327. Procedure Bright(ix,ij:integer);
  328. var sx:integer;
  329.     s:maxstring;
  330.  
  331. begin
  332.   s:=M^.Menu[ix,ij];
  333.   sx:=ix*Width+1;
  334.   writeat(sx+1,ij+1,Rev,s)
  335. end;
  336.  
  337.  
  338. Procedure UnderScore(ix,ij:integer);
  339. var sx:integer;
  340.     s:maxstring;
  341.  
  342. begin
  343.   sx:=ix*Width+1;
  344.   s:=M^.Menu[ix,ij];
  345.   writeat(sx+1,ij+1,Und,s)
  346. end;
  347.  
  348.  
  349. Procedure Normal(ix,ij:integer);
  350. var sx:integer;
  351.     s:maxstring;
  352.  
  353. begin
  354.   sx:=ix*Width+1;
  355.   if ij=0 then if sx<1 then sx:=1;
  356.   s:=M^.Menu[ix,ij];
  357.   writeat(sx+1,ij+1,Norm,s);
  358. end;
  359.  
  360.  
  361. Procedure PushUp(ix:integer);
  362. var sx,i:integer;
  363.  
  364. begin
  365.   sx:=ix*Width+1;
  366.   if sx<1 then sx:=1;
  367.   for i:=1 to M^.NumEntry[ix]+1 do
  368.      writeat(sx,i+1,Norm,'             ');
  369. end;
  370.  
  371. Procedure PullDown(ix:integer);
  372. const
  373.  
  374.     l:maxstring='║';
  375.     r:maxstring='║';
  376.  
  377. var sx:integer;
  378.     s:maxstring;
  379.     j:integer;
  380.  
  381. begin
  382. sx:=ix*Width+1;
  383. for j:=1 to M^.NumEntry[ix] do
  384.     begin
  385.     s:=l+M^.Menu[ix,j]+r;
  386.     writeat(sx,j+1,Norm,s);
  387.     end;
  388. if M^.NumEntry[ix]>0 then writeat(sx,M^.NumEntry[ix]+2,Norm,botbox);
  389. end;
  390.  
  391.  
  392. begin {DoMenu}
  393.  
  394. CursorOff;
  395.  
  396. validkeys:=[13,15,75,9,77,80,72,27];
  397.  
  398. entry:=1;
  399. item:=0;
  400. PaintMenuBar;
  401. PullDown(0);
  402. Bright(item,entry);
  403.  
  404. selection:=FALSE;
  405.  
  406. while not selection do
  407.    begin
  408.  
  409.    asc:= Inchar(ch,ex);
  410.  
  411.    if ex=0 then {Ctl-Brk hit}
  412.    begin
  413.       CursorNormal;
  414.       clrscr;
  415.       halt;
  416.    end;
  417.  
  418.    if not asc then
  419.    case ex{tended code} of
  420.  
  421.    13:{CR}
  422.       selection:=TRUE;
  423.  
  424.  
  425.       15, 75:{lefttab,left}
  426.       if item>0 then
  427.       begin
  428.         item:=item-1;
  429.         entry:=1;
  430.         pushup(item+1);
  431.         pulldown(item);
  432.         Bright(item,entry);
  433.       end;
  434.  
  435.        9, 77:{tab,right}
  436.       if item<M^.NoItems then
  437.         begin
  438.         item:=item+1;
  439.         entry:=1;
  440.         pushup(item-1);
  441.         pulldown(item);
  442.         entry:=1;
  443.         Bright(item,1);
  444.       end;
  445.  
  446.   80:{down}
  447.       begin
  448.         if entry<M^.NumEntry[item] then
  449.         begin
  450.           entry:=entry+1;
  451.           Normal(item,entry-1);
  452.           Bright(item,entry);
  453.         end
  454.         else
  455.         begin
  456.           entry:=1;
  457.           Normal(item,M^.NumEntry[item]);
  458.           Bright(item,entry);
  459.         end;
  460.       end;
  461.  
  462.  72:{up}
  463.      begin
  464.        if entry>1 then
  465.        begin
  466.          entry:=entry-1;
  467.          Normal(item,entry+1);
  468.          Bright(item,entry);
  469.        end
  470.        else
  471.        begin
  472.          entry:=M^.NumEntry[item];
  473.          Normal(item,1);
  474.          Bright(item,entry);
  475.        end;
  476.      end;
  477.  27:{Esc}
  478.      begin
  479.        selection:=TRUE;
  480.        item:=0;
  481.        entry:=0;
  482.      end;
  483.  
  484.  end;{case}
  485.  
  486. end;{while not selection}
  487. itemsel:=item;
  488. entrysel:=entry;
  489.  
  490. CursorNormal;
  491.  
  492. end;   {DoMenu}
  493.  
  494. { End of Pull-Down Window routines }
  495.  
  496.  
  497.                      { start  start  end    end    color  color }
  498.                      { col    line   col    line   fgnd   bgnd  }
  499. Procedure drawbox_ibm (x1,    y1,    x2,    y2,    FG,    BG : Integer;
  500.                        boxname : names; blnk : boolean);
  501. var
  502.   q : integer;
  503.   x : integer;
  504. Begin
  505.   window (x1,y1,x2,y1+1);
  506.   textbackground(BG);
  507.   GotoXY(1,1);
  508.   x := x2-x1;
  509.   if length(boxname) > x then boxname[0] := chr(x-4);
  510.   textcolor(FG);
  511.   Write('╒');
  512.   textcolor(fg);
  513.   write (boxname);
  514.   textcolor(FG);
  515.   for q := x1+length(boxname)+1 to x2-1 do Write('═');
  516.   Write('╕');
  517.   for q := 2 to y2-y1 do
  518.     Begin
  519.       window (x1,y1,x2,y1+q+1);
  520.       GotoXY(1,q); Write('│');
  521.       if blnk then clreol;
  522.       GotoXY(x2-x1+1,q); Write('│');
  523.     end;
  524.   Window(x1,y1,x2,y2+1);
  525.   gotoXY(1,y2-y1+1);
  526.   Write('╘');
  527.   for q := x1+1 to x2-1 do Write('═');
  528.   Write('╛');
  529. end;
  530.  
  531. Procedure drawbox (x1,y1,x2,y2,FG,BG : Integer;
  532.                    boxname : Names; blnk : boolean);
  533. Begin
  534.   Drawbox_IBM (x1,y1,x2,y2,FG,BG,boxname,blnk);
  535.   Window (x1+1,y1+1,x2-1,y2-1);
  536.   Clrscr;
  537. end;
  538.  
  539.  
  540. procedure beep;
  541. var
  542.    i    : integer;
  543.    tone : integer;
  544. begin
  545.  
  546.   tone:=200;
  547.   for i:=1 to 50 do
  548.   begin
  549.     sound(tone);
  550.     delay(1);
  551.     tone:=tone+10;
  552.   end;
  553.   nosound;
  554. end;
  555.  
  556. { The following are proprietory routines for TD.PAS }
  557.  
  558. function toupper(mess : ltype) : ltype;   { convert string to upper case }
  559. var
  560.   temp : ltype;
  561.   i    : integer;
  562.  
  563. begin
  564.   temp:='';
  565.   for i:=1 to length(mess) do
  566.     temp:=concat(temp,upcase(copy(mess,i,1)));
  567.   toupper:=temp;
  568. end;
  569.  
  570. function convert(num : integer) : stype;   { convert integer to string }
  571. var
  572.   st1  : stype;
  573.  
  574. begin
  575.   str(num,st1);
  576.   while copy(st1,1,1) = ' ' do
  577.     st1:=copy(st1,2,length(st1)-1);
  578.   convert:=st1;
  579. end;
  580.  
  581. procedure spread(c : char; l : integer);   { display specified # of characters }
  582. var
  583.  i : integer;
  584.  begin
  585.    for i:=1 to l do
  586.      write(c);
  587.  end;
  588.  
  589. procedure display_screen;  { display input file as a screen }
  590. var
  591.   i     : integer;
  592.   pause : char;
  593.   fgnd  : integer;
  594.  
  595. begin
  596.   clrscr;
  597.   for i := 1 to ndx-1 do
  598.   begin
  599.     gotoxy(fields[i].ry,fields[i].rx);
  600.     fgnd:=fields[i].rfgnd;
  601.     textcolor(fgnd);
  602.     if fields[i].rtype <> 'L' then
  603.     begin
  604.       if fields[i].rtype = 'A' then
  605.         spread('X',fields[i].rlen)
  606.       else
  607.       begin
  608.         spread('9',fields[i].rlen-fields[i].rscale);
  609.         if fields[i].rscale > 0 then
  610.         begin
  611.           write('.');
  612.           spread('9',fields[i].rscale);
  613.         end;
  614.       end;
  615.     end
  616.     else
  617.       write(fields[i].rname);
  618.   end;
  619. end;
  620.  
  621. procedure setup;    { prompt for and accept changes to screen }
  622. var
  623.   ans    : char;
  624.   iotype : string[8];
  625.   ftype  : char;
  626.   ch     : char;
  627.  
  628. begin
  629.   active := 1;
  630.   i := 1;
  631.   if fields[i].rtype = 'L' then
  632.   begin
  633.     repeat
  634.       i:=i+1;
  635.     until fields[i].rtype <> 'L';
  636.   end;
  637.   while i < ndx do
  638.   begin
  639.     display_screen;
  640.     gotoxy(fields[i].ry,fields[i].rx);
  641.     textcolor(fields[i].rfgnd+16);
  642.     if fields[i].rtype <> 'L' then
  643.     begin
  644.       if fields[i].rtype = 'A' then
  645.         spread('X',fields[i].rlen)
  646.       else
  647.       begin
  648.         spread('9',fields[i].rlen-fields[i].rscale);
  649.         if fields[i].rscale > 0 then
  650.         begin
  651.           write('.');
  652.           spread('9',fields[i].rscale);
  653.         end;
  654.       end;
  655.     end
  656.     else
  657.       write(fields[i].rname);
  658.     textcolor(15);
  659.  
  660.     domenu(item,entry,menus[active]);
  661.     index := active * 100 + item * 10 + entry;
  662.     gotoxy(1,23);
  663.     case index of
  664.  
  665.         101 :           { Next item }
  666.         begin
  667.           repeat
  668.             i := i + 1;
  669.           until (fields[i].rtype <> 'L') or (i >= ndx);
  670.         end;
  671.  
  672.         102 :           { Return to main menu }
  673.           i := ndx + 1;
  674.  
  675.         111:            { Alpha mode }
  676.           fields[i].rtype := 'A';
  677.  
  678.         112:            { Numeric mode }
  679.           fields[i].rtype := 'N';
  680.  
  681.         121:            { Length }
  682.         begin
  683.           gotoxy(1,1);
  684.           write('                                                                                ');
  685.           gotoxy(1,1);
  686.           write('Length = ',fields[i].rlen,'     Enter new length : ');
  687.           read(fields[i].rlen);
  688.         end;
  689.  
  690.         122:            { Scale }
  691.         if (fields[i].rtype = 'N') then
  692.         begin
  693.           gotoxy(1,1);
  694.           write('                                                                                ');
  695.           gotoxy(1,1);
  696.           write('Scale = ',fields[i].rscale,'    Enter new scale : ');
  697.           read(fields[i].rscale);
  698.         end;
  699.  
  700.         131:            { Color Menu }
  701.           active:=2;
  702.  
  703.         { 114,133 : }
  704.  
  705. { Color Pull-Down menu options }
  706.  
  707.         201 :           { Menu 1 }
  708.           active:=1;
  709.  
  710.         202..203:
  711.           fields[i].rfgnd := entry-1;
  712.         211 : fields[i].rfgnd := 3;
  713.         212 : fields[i].rfgnd := 4;
  714.         213 : fields[i].rfgnd := 5;
  715.         221 : fields[i].rfgnd := 6;
  716.         222 : fields[i].rfgnd := 7;
  717.         223 : fields[i].rfgnd := 8;
  718.         231 : fields[i].rfgnd := 9;
  719.         232 : fields[i].rfgnd := 10;
  720.         233 : fields[i].rfgnd := 11;
  721.         241 : fields[i].rfgnd := 12;
  722.         242 : fields[i].rfgnd := 13;
  723.         243 : fields[i].rfgnd := 14;
  724.         251 : fields[i].rfgnd := 15;
  725.         252 : fields[i].rfgnd :=  0;
  726.  
  727.     end   { case }
  728.   end;    { while i < ndx }
  729. end;
  730.  
  731. function min(a,b : integer) : integer;   { return the minimum of 2 numbers, or zero }
  732. begin
  733.   if a < b then
  734.     begin
  735.     if a > 0 then
  736.       min:=a
  737.     else min:=b;
  738.   end
  739.   else
  740.     begin
  741.     if b > 0 then
  742.       min:=b
  743.     else min:=a;
  744.   end;
  745. end;
  746.  
  747. function getvar(line : ltype) : ltype;   { breaks a token from input line }
  748. var
  749.   k     : integer;
  750.  
  751. begin
  752.   incr:=0;
  753.   if (copy(line,1,1)='!') or (copy(line,1,1)='#') then
  754.     begin
  755.     k:=pos(' ',line);
  756.     if k = 0 then
  757.       getvar:=line
  758.     else
  759.       begin
  760.       incr:=k-1;
  761.       getvar:=(copy(line,1,k-1))
  762.     end;
  763.   end
  764.   else
  765.     begin
  766.     k:=min(pos('!',line),pos('#',line));
  767.     if k=0 then
  768.       getvar:=line
  769.     else
  770.       begin
  771.       incr:=k-1;
  772.       getvar:=copy(line,1,k-1);
  773.     end;
  774.   end;
  775. end;
  776.  
  777. function deblank(str1 : stype) : stype;  { remove excess characters from the end of a string }
  778. var
  779.   str2      : stype;
  780.   c         : char;
  781.   i         : integer;
  782.  
  783. label 99;
  784.  
  785. begin
  786.   str2:=str1;
  787.   if (copy(str2,1,1)='!') or (copy(str2,1,1)='#') then
  788.     str2:=copy(str2,2,(length(str2)-1)+1);
  789.   for i:=length(str2) downto 1 do
  790.     begin
  791.     if copy(str2,i,1) <> ' ' then
  792.       goto 99;
  793.   end;
  794. 99:
  795.   deblank:=copy(str2,1,i);
  796. end;
  797.  
  798. function verify(st2 : ltype) : integer;  { return position of 1st non-space }
  799. var
  800.   i    : integer;
  801. label gotit;
  802.  
  803. begin
  804.   for i:=1 to length(st2) do
  805.    if copy(st2,i,1) <> ' ' then
  806.      goto gotit;
  807.  
  808. gotit:
  809.   if i=length(st2) then  { all spaces }
  810.     verify:=0
  811.   else
  812.     verify:=i;
  813. end;
  814.  
  815.  
  816. Procedure menu;   { opening menu }
  817. var
  818.   continue : boolean;
  819.  
  820. Begin
  821.   continue:=true;
  822.   active:=3;
  823.   while continue = true do
  824.     begin
  825.     Clrscr;
  826.     textcolor(15);
  827.     highvideo;
  828.     Gotoxy(11,4);
  829.     Write('Copyright (c) 1985  Mark E.Johnson - MicroTools Co.');
  830.     Gotoxy(1,2);
  831.     Write(' ');
  832.     Gotoxy(25,6);
  833.     Write('TurboDraw 3.12');
  834.     Gotoxy(27,7);
  835.     Write('File Menu');
  836.     Gotoxy(16,9);
  837.     Write('Screen Input File   ');
  838.     lowvideo;
  839.     Gotoxy(40,9);
  840.     Write(inname);
  841.     highvideo;
  842.     Gotoxy(16,10);
  843.     Write('Pascal Output File  ');
  844.     lowvideo;
  845.     Gotoxy(40,10);
  846.     Write(outname);
  847.     highvideo;
  848.     Gotoxy(16,11);
  849.     Write('Library Input File  ');
  850.     lowvideo;
  851.     Gotoxy(40,11);
  852.     Write(libname);
  853.     highvideo;
  854.     domenu(item,entry,menus[active]);
  855.     index := active*100 + item*10 + entry;
  856.     case index of
  857.       300 : ans := '4';
  858.       301 : ans := '4';
  859.       311 : ans := '1';
  860.       321 : ans := '2';
  861.       331 : ans := '3';
  862.     end;
  863.     if ans='4' then
  864.       continue:=false
  865.     else
  866.       begin
  867.       Gotoxy(16,14);
  868.       Write('Enter File name or <C/R>   ')
  869.     end;
  870.     if ans='1' then
  871.       begin
  872.       lowvideo;
  873.       gotoxy(40,9);
  874.       write('               ');
  875.       gotoxy(40,9);
  876.       readln(inname);
  877.       highvideo;
  878.       inname:=toupper(inname);
  879.     end
  880.     else if ans='2' then
  881.       begin
  882.       lowvideo;
  883.       gotoxy(40,10);
  884.       write('               ');
  885.       gotoxy(40,10);
  886.       readln(outname);
  887.       highvideo;
  888.       outname:=toupper(outname)
  889.     end
  890.     else if ans='3' then
  891.       begin
  892.       lowvideo;
  893.       gotoxy(40,11);
  894.       write('               ');
  895.       gotoxy(40,11);
  896.       readln(libname);
  897.       highvideo;
  898.       libname:=toupper(libname)
  899.     end;
  900.   end;
  901. End;
  902.  
  903. procedure wrname(i : integer);    { display a variable or literal }
  904. var
  905.   x : integer;
  906. begin
  907.   for x:=1 to 20 do
  908.     if x <= length(fields[i].rname) then
  909.       write(copy(fields[i].rname,x,1));
  910. end;
  911.  
  912. procedure sort;     { display and/or sort individual fields for order of input/output }
  913. var
  914.     hfield    : fieldrec;
  915.  
  916.     litvar,iotype,ftype : stype;
  917.     junk      : char;
  918.     ord1,ord2 : integer;
  919.     i,j       : integer;
  920.     again,l1  : boolean;
  921.  
  922. label ordl,endsort;
  923.  
  924. begin
  925.     while true do
  926.         begin
  927.         clrscr;
  928.         lowvideo;
  929.         write('Order Field Name                Field     Mode    Type    Line   Col  Color');
  930.         highvideo;
  931.         j:=1;
  932.         for i:=1 to ndx-1 do
  933.             begin
  934.             if j > 18 then
  935.                 begin
  936.                 j:=1;
  937.                 gotoxy(1,22);
  938.                 write('Press a key to continue ');
  939.                 read(kbd,junk);
  940.                 clrscr;
  941.                 lowvideo;
  942.                 writeln('Order Field Name                Field     Mode    Type    Line   Col  Color');
  943.                 highvideo;
  944.  
  945.             end;
  946.  
  947.             case fields[i].rtype of
  948.               'L' : begin
  949.                       litvar := 'Literal';
  950.                       iotype := 'Output';
  951.                       ftype  := 'Alpha';
  952.                     end;
  953.               'A' : begin
  954.                       litvar := 'Variable';
  955.                       ftype  := 'Alpha';
  956.                     end;
  957.  
  958.               'N' : begin
  959.                       litvar := 'Variable';
  960.                       ftype  := 'Numeric';
  961.                     end;
  962.             end;
  963.  
  964.             case fields[i].rio of
  965.               'I' :  iotype := 'Input';
  966.               'O' :  iotype := 'Output';
  967.             end;
  968.  
  969.             if fields[i].rname <> '' then  { don't display blank lines }
  970.                 begin
  971.                 gotoxy(1,j+1);
  972.                 lowvideo;
  973.                 write(fields[i].rorder:3);
  974.                 highvideo;
  975.                 gotoxy(7,j+1);
  976.                 textcolor(fields[i].rfgnd);
  977.                 wrname(i);
  978.                 textcolor(15);
  979.                 gotoxy(32,j+1);
  980.                 write(litvar);
  981.                 gotoxy(42,j+1);
  982.                 write(iotype);
  983.                 gotoxy(50,j+1);
  984.                 write(ftype);
  985.                 gotoxy(60,j+1);
  986.                 write(fields[i].rx:2);
  987.                 gotoxy(66,j+1);
  988.                 write(fields[i].ry:2);
  989.                 gotoxy(73,j+1);
  990.                 write(fields[i].rfgnd:2);
  991.                 j:=j+1;
  992.             end;
  993.         end;
  994.         L1:=TRUE;
  995.         repeat
  996.             gotoxy(1,22);
  997.             write('Enter field to change, or 999 to quit    ');
  998.             lowvideo;
  999.             gotoxy(1,23);
  1000.             write('      ');
  1001.             gotoxy(1,23);
  1002.             readln(ord1);
  1003.             highvideo;
  1004.             if ord1=999 then
  1005.                 goto endsort;
  1006.             for j:=1 to ndx-1 do
  1007.                 if ord1=fields[j].rorder then
  1008.                     goto ordl;
  1009.  ordl:      if ord1 = fields[j].rorder then
  1010.                 l1:=FALSE;
  1011.         until l1 = false;
  1012.         ord1:=j;
  1013.         gotoxy(1,22);
  1014.         write('Place at field #                        ');
  1015.         lowvideo;
  1016.         gotoxy(1,23);
  1017.         write('     ');
  1018.         gotoxy(1,23);
  1019.         readln(ord2);
  1020.         highvideo;
  1021.         fields[ord1].rorder:=ord2;
  1022.  
  1023.  { Simple bubble sort is fast enough for this application }
  1024.  
  1025.         Again:=TRUE;
  1026.         while again = true do
  1027.             begin
  1028.             Again:=FALSE;
  1029.  
  1030.             for i:=1 to ndx-2 do
  1031.                 begin
  1032.                 If fields[i].rorder > fields[i+1].rorder Then
  1033.                     begin
  1034.                     hfield      := fields[i];
  1035.                     fields[i]   := fields[i+1];
  1036.                     fields[i+1] := hfield;
  1037.                     again:=TRUE;
  1038.                 end;
  1039.             end;
  1040.  
  1041.         end;
  1042.      end;
  1043.     endsort:
  1044.  End;
  1045.  
  1046.  
  1047. begin { main }
  1048.   CursorNormal;
  1049.  
  1050.   SetVideoSeg;
  1051.   LoadMenus(Menus);
  1052.   inname:='TEST.SCR';
  1053.   outname:='TEST.PAS ';
  1054.   libname:='TD.LIB';
  1055.   beep;
  1056. retry:
  1057.   menu;
  1058.   level:=0;
  1059.   incr:=0;
  1060.   varfl:=true;
  1061.   librfl:=false;
  1062.   subrfl:=false;
  1063.   outtype:='C';
  1064.   ndx:=1;
  1065.   lineno:=1;
  1066.   assign(infile,inname);
  1067.   {$I-}
  1068.   reset(infile);
  1069.   {$I+}
  1070.   if ioresult <> 0 then
  1071.   begin
  1072.     drawbox(40,4,77,8,7,0,'[ Error ]',true);
  1073.     beep;
  1074.     writeln('Screen file not found,');
  1075.     write('Press a key to continue ');
  1076.     read(kbd,ans);
  1077.     window(40,1,77,4);
  1078.     clrscr;
  1079.     window(1,1,80,25);
  1080.     goto retry
  1081.   end;
  1082.  
  1083.   assign(outfile,outname);
  1084.   rewrite(outfile);
  1085.  
  1086.   efile:=false;
  1087.   while efile = false do
  1088.     begin
  1089.     colno:=1;
  1090.     readln(infile,line);
  1091.     if eof(infile) then
  1092.       efile:=true;
  1093.     l:=length(line);
  1094.     i:=0;
  1095.     while colno < l do
  1096.       begin
  1097.       i:=verify(line);
  1098.       if (i=0) and (length(line) > 0) then
  1099.         i:=1;
  1100.       if i > 0 then
  1101.         begin
  1102.         colno:=colno+i+incr-1;
  1103.         token:=GETVAR(copy(line,i,(length(line)-i)+1));
  1104.         j:=i+length(token);
  1105.         fields[ndx].rtype:='L';
  1106.         fields[ndx].rio  :='O';
  1107.         if copy(token,1,1) = '!' then
  1108.           begin
  1109.           fields[ndx].rio   := 'O';
  1110.           fields[ndx].rtype := 'N';
  1111.           token:=copy(token,2,length(token)-1);
  1112.         end
  1113.         else if copy(token,1,1) = '#' then
  1114.           begin
  1115.           fields[ndx].rtype := 'N';
  1116.           fields[ndx].rio   := 'I';
  1117.           token:=copy(token,2,length(token)-1);
  1118.         end;
  1119.         fields[ndx].rname:= token;
  1120.         fields[ndx].rx:=lineno;
  1121.         fields[ndx].ry:=colno;
  1122.         if fields[ndx].rtype <> 'A' then  { alphanumeric fields default to zero length }
  1123.           fields[ndx].rlen:=length(token)
  1124.         else
  1125.           fields[ndx].rlen:=0;
  1126.         fields[ndx].rscale:=0;
  1127.         if fields[ndx].rtype <> 'L' then
  1128.           fields[ndx].rfgnd:=7
  1129.         else
  1130.           fields[ndx].rfgnd:=15;
  1131.         fields[ndx].rorder:=ndx*10;
  1132.         if j >= length(line) then
  1133.           l:=0
  1134.         else
  1135.           line:=copy(line,j,(length(line)-j)+1);
  1136.         ndx:=ndx+1;
  1137.       end;
  1138.     end;
  1139.     lineno:=lineno+1;
  1140.   end;
  1141.  
  1142. endinp:
  1143.   close(infile);
  1144.   while true do
  1145.   begin
  1146.     active:=4;   { Set menu level }
  1147.     clrscr;
  1148.     Gotoxy(11,6);
  1149.     Write('Copyright (c) 1985  Mark E.Johnson - MicroTools Co.');
  1150.     Gotoxy(1,2);
  1151.     Write(' ');
  1152.     Gotoxy(25,8);
  1153.     Write('TurboDraw 3.12');
  1154.     gotoxy(28,9);
  1155.     write('OPTIONS');
  1156.     lowvideo;
  1157.     gotoxy(19,12);
  1158.     write('Include Library functions');
  1159.     highvideo;
  1160.     gotoxy(50,13);
  1161.     if librfl = true then
  1162.       write('Yes')
  1163.     else
  1164.       write(' No');
  1165.     lowvideo;
  1166.     gotoxy(19,14);
  1167.     write('Generate a procedure');
  1168.     highvideo;
  1169.     gotoxy(50,14);
  1170.     if subrfl = true then
  1171.     begin
  1172.       write('Yes  (');
  1173.       write(procname,')');
  1174.     end
  1175.       else write(' No');
  1176.     lowvideo;
  1177.     gotoxy(19,15);
  1178.     write('Include VAR Definitions');
  1179.     highvideo;
  1180.     gotoxy(50,15);
  1181.     if varfl = true then write('Yes')
  1182.       else write(' No');
  1183.  
  1184.     domenu(item,entry,menus[active]);
  1185.     index := active*100 + item*10 + entry;
  1186.  
  1187.     case index of
  1188.             412:    begin    { Procedure }
  1189.                       subrfl:= NOT subrfl;
  1190.                       if subrfl=true then
  1191.                         begin
  1192.                         drawbox(19,20,60,23,7,0,'[ Proc ]',false);
  1193.                         write('Enter name of procedure ');
  1194.                         textcolor(15);
  1195.                         read(procname);
  1196.                         window(19,20,60,23);
  1197.                         clrscr;
  1198.                         window(1,1,80,25);
  1199.                       end
  1200.                     end;
  1201.  
  1202.               411:  begin    { Include library }
  1203.                      librfl:=NOT librfl;
  1204.                      if librfl=true then
  1205.                       begin
  1206.                        assign(libfile,'TD.LIB');
  1207.                        {$I-}
  1208.                        reset(libfile);
  1209.                        {$I+}
  1210.                        if ioresult <> 0 then
  1211.                        begin
  1212.                          drawbox(20,2,55,6,7,0,'[ Error ]',true);
  1213.                          beep;
  1214.                          writeln('For this option, you must have ');
  1215.                          writeln(libname,' on the default drive.');
  1216.                          write  ('  Press a key to continue ');
  1217.                          read(kbd,ans);
  1218.                          window(20,6,55,10);
  1219.                          clrscr;
  1220.                          window(1,1,80,25);
  1221.                          librfl:=false;
  1222.                          textcolor(15);
  1223.                        end;
  1224.                      end
  1225.                    end;
  1226.  
  1227.              413:  varfl:=NOT varfl;
  1228.              403:  goto Generate;
  1229.              401:  Setup;
  1230.              402:  sort;
  1231.  
  1232.   end;
  1233. end;
  1234.  
  1235.        { Generate Code for TURBO PASCAL }
  1236.  
  1237. generate:
  1238.  
  1239. writeln(outfile);
  1240. writeln(outfile,'{ Start of Turbodraw code }');
  1241.   if varfl = true then
  1242.     begin
  1243.     writeln(outfile,'Var');
  1244.     for i:=1 to ndx-1 do
  1245.       begin
  1246.       if fields[i].rtype <> 'L' then
  1247.         begin
  1248.         writeln(outfile);
  1249.         write(outfile,'  ',fields[i].rname);
  1250.         if fields[i].rtype = 'N' then
  1251.         begin
  1252.           if fields[i].rscale > 0 then
  1253.             write(outfile,' : Real;')
  1254.           else
  1255.             write(outfile,' : Integer;');
  1256.         end
  1257.         else if fields[i].rtype = 'A' then
  1258.           write(outfile,' : String[',convert(fields[i].rlen),'];');
  1259. {       else
  1260.           begin
  1261.           if fields[i].rscale > 0 then
  1262.             write(outfile,' : Real;')
  1263.           else
  1264.             write(outfile,' : Integer;');
  1265.         end;
  1266. }     end;
  1267.     end;
  1268.     writeln(outfile);
  1269.   end;
  1270.   writeln(outfile);
  1271.  
  1272.  if librfl = true then   { output library file }
  1273.     begin
  1274.     while not eof(libfile) do     { Include library code }
  1275.       begin
  1276.       readln(libfile,line);
  1277.       writeln(outfile,line);
  1278.     end;
  1279.   close(libfile)
  1280.   end;
  1281.   if subrfl = true then
  1282.     begin
  1283.     writeln(outfile);
  1284.     writeln(outfile,'Procedure ',procname,';');
  1285.     writeln(outfile,'Begin');
  1286.     writeln(outfile,'  Clrscr;');
  1287.     writeln(outfile,'  TextColor(15);');
  1288.   end;
  1289.   for i:=1 to ndx-1 do
  1290.     begin
  1291.     if fields[i].rname > ' ' then
  1292.       writeln(outfile,'  Gotoxy(',convert(fields[i].ry),',',convert(fields[i].rx),');');
  1293.     if fields[i].rtype = 'L' then
  1294.       begin
  1295.       if fields[i].rname > ' ' then
  1296.       begin
  1297.         if lastcolor <> 15 then
  1298.         begin
  1299.           lastcolor:=15;
  1300.           writeln(outfile,'  TextColor(15);');
  1301.         end;
  1302.         writeln(outfile,'  Write(''',fields[i].rname,''');');
  1303.       end;
  1304.     end
  1305.     else if fields[i].rio = 'O' then
  1306.       begin
  1307.       tail:=convert(fields[i].rlen);
  1308.       tail:=concat(':',tail);
  1309.       if fields[i].rscale > 0 then
  1310.         tail:=concat(tail,':',convert(fields[i].rscale));
  1311.       tail:=concat(tail,');');
  1312.       if lastcolor <> fields[i].rfgnd then
  1313.       begin
  1314.         lastcolor:=fields[i].rfgnd;
  1315.         writeln(outfile,'  TextColor(',convert(fields[i].rfgnd),');');
  1316.       end;
  1317.       if (fields[i].rtype = 'A') or (fields[i].rlen = 0) then
  1318.         writeln(outfile,'  Write(',fields[i].rname,');')
  1319.       else
  1320.         writeln(outfile,'  Write(',fields[i].rname,tail)
  1321.     end
  1322.  
  1323.     else if fields[i].rio = 'I' then
  1324.       begin
  1325.       if lastcolor <> fields[i].rfgnd then
  1326.       begin
  1327.         lastcolor:=fields[i].rfgnd;
  1328.         writeln(outfile,'  TextColor(',convert(fields[i].rfgnd),');');
  1329.       end;
  1330.       if (fields[i].rtype = 'A') or (fields[i].rlen = 0) then
  1331.         writeln(outfile,'  Read(',fields[i].rname,');')
  1332.       else
  1333.       if fields[i].rscale > 0 then
  1334.         writeln(outfile,'  ',fields[i].rname,':=Getreal(',convert(fields[i].rlen),',',convert(fields[i].rscale),');')
  1335.       else
  1336.         writeln(outfile,'  ',fields[i].rname,':=Getint(',convert(fields[i].rlen),');');
  1337.     end;
  1338.   end;
  1339.   if lastcolor <> 15 then
  1340.     writeln(outfile,'  TextColor(15);');
  1341.   if subrfl = true then
  1342.     writeln(outfile,'End;');
  1343.   writeln(outfile,'{ End of Turbodraw Code }');
  1344.   writeln(outfile);
  1345.   close(outfile);
  1346. end.
  1347.